#!/bin/bash

# 下载地址：https://download.sonatype.com/nexus/3/latest-unix.tar.gz

source /home/shell/util.sh

local_url=http://192.168.100.59/download/nexus

requireJava
source /etc/profile.d/java.sh


read -p "请输入端口[8081]：" nexus_port
nexus_port=${nexus_port:-"8081"};

mkdir -p /opt/nexus
useradd nexus -d /opt/nexus/

cd /opt/nexus

function getFromLocal () {
  rz
  if [ ! -f "/opt/nexus/latest-unix.tar.gz" ]; then
    printFail "安装文件不存在，退出安装程序！"
    exit
  fi
  mv *.tar.gz latest-unix.tar.gz
}

function getFromLAN () {
  printInfo "版本列表："
  curl -sL ${local_url}|grep -e "\s*M"|perl -pe "s/.*>(.*)<.*/\1/g"
  while [ true ]; do
      read -p "请选择版本：" version
      if [[ -z "${version}" ]]; then
          printFail "请重新输入版本！"
          continue;
      else
          break;
      fi
  done
  if [ ! -f "/opt/nexus/latest-unix.tar.gz" ]; then
    download /opt/nexus/latest-unix.tar.gz ${local_url}/${version}
  else
    printWarn "安装包已存在，跳过下载。"
  fi
}

cat <<EOF
  1. 从局域网安装
  2. 从本地上传安装
EOF
while [ true ]; do
  read -p "请选择：" install_option
  case "${install_option}" in
    1)
      getFromLAN
      break;
    ;;
    2)
      getFromLocal
      break;
    ;;
    *)
      printFail "选择错误，重新选择。"
    ;;
  esac
  
done

# rz
# if [ ! -f "/opt/nexus/latest-unix.tar.gz" ];then
#   download /opt/nexus/latest-unix.tar.gz http://yonghecar.cn/mystatic/latest-unix.tar.gz
#   # echo -e '未找到nexus压缩包，程序退出!\n'
#   # exit;
# fi

tar -xf latest-unix.tar.gz
mv /opt/nexus/nexus-* /opt/nexus/nexus
chown -R nexus:nexus /opt/nexus

# ln -s /opt/nexus/nexus/bin/nexus /etc/init.d/nexus3
ln -s /opt/nexus/nexus/bin/nexus /usr/bin/nexus

sed -ri "s/# INSTALL4J_JAVA_HOME_OVERRIDE=/INSTALL4J_JAVA_HOME_OVERRIDE=\/opt\/jdk/" /opt/nexus/nexus/bin/nexus

sed -ri "s/#run_as_user=\"\"/run_as_user=\"nexus\"/" /opt/nexus/nexus/bin/nexus.rc
bash /home/shell/serviced-install.sh<<EOF
2


nexus
/opt/nexus/nexus/bin/nexus start

/opt/nexus/nexus/bin/nexus restart
/opt/nexus/nexus/bin/nexus stop
nexus
EOF

loadmsg_time=0;
while [ ! -f "/opt/nexus/sonatype-work/nexus3/etc/nexus.properties" ]; do
  loadMsg "系统启动中，请稍后..."
  sleep 1s
done

sed -ri "s/# application-port=8081/application-port="$nexus_port"/g" /opt/nexus/sonatype-work/nexus3/etc/nexus.properties

# chkconfig --add nexus3
# chkconfig nexus3 on

# systemctl start nexus3

firewall-addport 5000
firewall-addport $nexus_port
systemctl restart nexus

# 清理
rm -rf /opt/nexus/latest-unix.tar.gz

loadmsg_time=0;
while [ ! -f "/opt/nexus/sonatype-work/nexus3/admin.password" ]; do
  loadMsg "系统启动中，请稍后..."
  sleep 1s
done
loadmsg_time=0;
while [ true ]; do
  loadMsg "nexus即将完成安装，请稍后..."
  msg=`tail -10 /opt/nexus/sonatype-work/nexus3/log/nexus.log|grep "Started Sonatype Nexus OSS"`;
  if [[ -n "${msg}" ]]; then
    break;
  fi
  sleep 1s
done

printInfo "==========================================="
printInfo "admin默认密码：`cat /opt/nexus/sonatype-work/nexus3/admin.password`"
printInfo "日志文件：/opt/nexus/sonatype-work/nexus3/log/nexus.log"
printInfo "后台管理地址为：http://$host_addr:${nexus_port}"
printInfo "==========================================="

# tail -200f /opt/nexus/sonatype-work/nexus3/log/nexus.log